#pragma once
#include "dbng.hpp"
#include "../../DB/DBManager.h"
#include  "../Common/Common.h"

#include "include\wrapper\cef_message_router.h"

#include <string>
#include <vector>
#include <memory>
using namespace std;
using namespace ormpp;

struct Role{
    int id;
    string name;
    
};
REFLECTION(Role, id, name)

class RoleManager:public CefRefCount
{
private:
    static CefRefPtr<RoleManager> role;
public:
    RoleManager(){}
    ~RoleManager(){}

    static bool process(string action, CefRefPtr<CefDictionaryValue> data,CefRefPtr<CefBrowser> browser,
        CefRefPtr<CefMessageRouterBrowserSide::Callback> callback){
        if(action == "import"){
            callback->Success("{}");           
        }
        else if(action == "add"){
            Role role=CefDictionary2Struct<Role>(data);
            DBManager::inst()->insert(role);
            callback->Success("{}");
        }else if(action == "del"){
            Role role=CefDictionary2Struct<Role>(data);
            DBManager::inst()->delete_records<Role>("name='"+role.name+"'");
            callback->Success("{}");
        }else if(action == "query"){
            iguana::string_stream ss;
            vector<Role> item = DBManager::inst()->query<Role>();
            iguana::json::to_json(ss, item);
            callback->Success(ss.str());
        }
        return false;
    }
};
